<template>
  <div>
    <div class="form-wrapper">
      <el-form ref="form" :model="form" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="用户名">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="tool-wrapper">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <div class="table-wrapper">
      <el-table :data="tableData" v-loading="tableLoading" height="600" border style="width: 100%"  @selection-change="changeFun" >
        <!-- <el-table-column prop="id" label="ID"></el-table-column> -->
        <el-table-column type="index" :index="indexMethod" label="序号"  width="50"></el-table-column>
        <el-table-column prop="id" label="id"></el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="pwd" label="密码"></el-table-column>
        <el-table-column prop="email" label="登陆邮箱"></el-table-column>
        <el-table-column prop="nickname" label="昵称"></el-table-column>
      </el-table>
      <el-pagination  background layout="total,sizes,prev, pager, next"  :page-sizes="[10, 20, 50, 100]" :total="form.total" :current-page="form.page"  @current-change="handleCurrentChange" @size-change="handleSizeChange"></el-pagination>
    </div>
    <div class="modal-wrapper">
      <div class="modal-wrapper">
        <!--  -->
        <el-dialog title="新增" :visible.sync="addVisible">
          <el-form :model="addform" ref="addform" :rules="addRules" label-width="100px">
            <el-row :gutter="20" class="mb20">
              <el-col :span="12">
                <el-form-item label="网关sn">
                  <el-input type="text" v-model="addform.sn"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="设备名称">
                  <el-input type="text" v-model="addform.title"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="addVisible = false">取消</el-button>
            <el-button type="primary" @click="addSubmit('addform')">确定</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'components',
  data () {
    return {
      tableData: [],
      tableLoading: false,
      form: {
        rows: 10,
        page: 1,
        total: 1,
        name: ''
      },

      addVisible: false,
      addform: {},
      addRules: {}
    };
  },
  created () {
    this.tableDataLoad();
  },
  methods: {
    add () {
      this.addform = {};
      this.addVisible = true;
    },
    addSubmit () {
      this.$api.post('/admin/dydcHouseDevice/editorDevice.do', this.addform, r => {
        this.addVisible = false;
        this.tableDataLoad();
        if (r.success) {
          this.$message({
            message: '新增/修改成功',
            type: 'success'
          });
        } else {
          this.$message({
            message: '新增/修改失败',
            type: 'error'
          });
        }
      });
    },
    search () {
      // 搜索
      this.form.page = 1;
      this.tableDataLoad();
    },
    handleCurrentChange (val) {
      // 表格页码变化时
      this.form.page = val;
      this.tableDataLoad();
    },
    handleSizeChange (val) {
      // 每页显示个数变化事
      this.form.rows = val;
      this.tableDataLoad();
    },
    changeFun (val) {
      this.choose = val;
    },
    tableDataLoad () {
      this.tableLoading = true;// 加载更新
      // 加载表格数据
      this.$api.post('/api/user.php', this.form, r => {
        this.tableLoading = false;// 加载更新
        this.tableData = r.rows;
        this.form.total = r.total;
      });
    },
    indexMethod (index) {
      return index + ((this.form.page - 1) * this.form.rows) + 1;
    }
  }
};
</script>

<style lang="stylus" scoped>
.form-wrapper
  margin-bottom 20px
  padding 0 20px 20px
  margin-left -20px
  margin-right -20px
  border-bottom 1px solid #ececec;
.tool-wrapper
  margin-bottom 20px
.el-pagination
  padding 20px
  text-align right
</style>
